<?php

declare(strict_types=1);

namespace App\Model;

use App\Helpers\AliyunUtil;
use App\Helpers\ConfigUtil;
use App\Helpers\Helper;
use App\Helpers\ImUtil;
use App\Model\Setting\SettingAppHidden;
use App\Model\Setting\SettingUserLevel;
use Hyperf\DbConnection\Model\Model;

/**
 * 消息记录
 */
class Message extends Model
{
    //透传消息类型
    const SILENT_MSG_TYPE = [
        //金币不足剩余N分钟通话
        'LACK_COIN' => 'msg_lack_coin',
        //金币消耗完
        'NO_COIN' => 'msg_no_coin',
        //金币足够了
        'ENOUGH_COIN' => 'msg_enough_coin',
        //切换主播（新手福利）
        'CHANGE_FOR_NEW' => 'msg_change_host_for_new',
        //切换主播（一键语聊）
        'CHANGE_FOR_QUICK' => 'msg_change_host_for_quick',
        //私聊数限制
        'CHAT_LIMIT' => 'msg_chat_limit',
        //挂断电话-主叫方
        'HANGUP_CALL' => 'msg_hangup_call',
        //挂断电话-被叫方
        'HANGUP_CALLED' => 'msg_hangup_called',
        //当前通话已结束
        'CALL_OVER' => 'msg_call_over',
        //上线提醒
        'ONLINE_TIP' => 'msg_online_tip',
        //新消息提醒
        'NEW_NOTICE' => 'msg_new_notice',
        //自动领取礼物成功
        'AUTO_RECEIVE_GIFT' => 'msg_auto_receive_gift',
        //礼物已失效
        'AUTO_EXPIRE_GIFT' => 'msg_auto_expire_gift',
        //充值礼包'
        'RECHARGE_TIP' => 'msg_pop_recharge_tip',
        //账户余额变动
        'USER_COIN_CHANGE' => 'msg_user_coin_change',
        //动态消息
        'MOMENT_NEW_MSG' => 'msg_moment_msg',
        //呼叫充值引导
        'CALL_VIDEO_TO_PAY' => 'msg_call_video_to_pay',
        //礼物推送
        'SEND_GIFT_TIP' => 'msg_send_gift_tip',
        //引导主播打视频
        'HOST_CALL_VIDEO_TIP' => 'msg_host_call_video_tip',
        //清除聊天记录
        'CLEAR_MSG' => 'msg_clear_msg_list',
        //搭讪提醒
        'HIT_LOOP' => 'msg_hit_loop',
        //视频礼物
        'VIDEO_GIFT_TIP' => 'msg_video_gift_tip',
        //额度不足充值提示弹窗
        'FREE_BALANCE_TIP' => 'msg_account_balance_isinsufficient_tip',
        //通话邀请弹窗
        'CALL_INVITE' => 'msg_call_invitation_tip',
        // 视频通话涉黄
        'VIDEO_YELLOW_TIP' => 'video_yellow_tip',
        //每日任务未完成提示
        'DAILY_TASK_TIP' => 'daily_task_tip',
        //每日任务完成提示
        'DAILY_TASK_FINISH_TIP' => 'daily_task_finish_tip',
        // 新人女用户礼包
        'NEW_GIRL_REPLY' => 'new_girl_reply',
        // 填写搭讪三件提示
        'USER_AUTO_MSG' => 'user_auto_msg',
        // 资料是否填写完整(未填写弹框)
        'IMPROVE_INFO' => 'improve_info',
        // 资料是否填写完整(完善资料弹框)
        'IMPROVE_INFO_FINISH' => 'improve_info_finish',
        // 提现提示
        'WITHDRAW_CAN' => 'withdraw_can',
        // 真人认证红包
        'REAL_AVATAR_POP' => 'real_avatar_pop',
    ];

    //通知类型 - 系统通知
    const PUSH_TYPE_SYSTEM = 'SYSTEM';
    //通知类型 - 官方通知
    const PUSH_TYPE_OFFICIAL = 'OFFICIAL';

    //站内信通知类型
    const MSG_TYPE_PIC = 'PIC';
    const MSG_TYPE_PIC_TEXT = 'PIC_TEXT';
    const MSG_TYPE_TEXT = 'TEXT';
    const MSG_TYPE_TEXT_LINK = 'TEXT_LINK';
    const MSG_TYPE_WECHAT_APPLY = 'WECHAT_APPLY';
    const MSG_TYPE = [
        self::MSG_TYPE_PIC => '纯图带跳转消息',
        self::MSG_TYPE_PIC_TEXT => '图文带跳转消息',
        self::MSG_TYPE_TEXT => '纯文字消息（有标题）',
        self::MSG_TYPE_TEXT_LINK => '纯文字带跳转消息（有标题）',
        self::MSG_TYPE_WECHAT_APPLY => '查看微信申请消息',
    ];

    //官方消息分类
    const MSG_CATE_SYS = 1;
    const MSG_CATE_VRFY = 2;
    const MSG_CATE_WECHAT_APPLY = 3;
    const MSG_CATE_XIAOMISHU = 4;
    const MSG_CATE = [
        self::MSG_CATE_SYS => '系统消息',
        self::MSG_CATE_VRFY => '审核通知',
        self::MSG_CATE_WECHAT_APPLY => '微信申请',
        self::MSG_CATE_XIAOMISHU => '小秘书'
    ];

    //消息列表标签
    const  MSG_TAG = [
        1 => ['tag_type' => 'video', 'tag_name' => '视频过'],
        ['tag_type' => 'chat', 'tag_name' => '互动过'],
        ['tag_type' => 'voice', 'tag_name' => '语音过'],
        ['tag_type' => 'city', 'tag_name' => '同城']
    ];

    /**
     * 发送透传消息
     * @param $pushUserId
     * @param array $msgInfo
     * @param array $sender
     */
    static function sendSlient($userIds = [], $msgType = '', $extInfo = [])
    {
        if (empty($userIds)) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $accounts = User::getImAccounts($userIds);
        //消息数据
        $msgInfo =  ['msg_obj' => $extInfo, 'msg_type' => self::SILENT_MSG_TYPE[$msgType]];
        //发送透传
        return ImUtil::sendSilentMsg($accounts, $msgInfo);
    }

    /**
     * 发送礼物消息
     */
    static function sendGift(
        $userIds = [],
        $fromUserId = 0,
        $giftInfo = [],
        $needFetch = false,
        $senderShow = 0,
        $showTip = '',
        $newGift = false
    ) {
        if (empty($userIds) || !$fromUserId || !$giftInfo) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $userIds = array_merge($userIds, [$fromUserId]);
        $accounts = User::getImAccounts($userIds);
        $fromAccount = $accounts[$fromUserId] ?? '';
        unset($accounts[$fromUserId]);

        $tip = $showTip ?: ($needFetch ? '提示:给对方发消息即可领取礼物哦~' : '');


        $msgInfo = self::buildGiftMsgObj($giftInfo, $tip, 1, GiftLog::STATUS_WAIT);
        if ($newGift){
            $msgInfo['new_tip'] = '新人礼包回复领现金';
        }
        return ImUtil::sendCustomMsg($accounts, $fromAccount, ImUtil::MSG_TYPE_GIFT, $msgInfo, '发来一个打招呼礼物', $senderShow);
    }

    /**
     * 发送礼物消息
     */
    static function sendGiftReceived($userIds = [], $fromUserId = 0, $giftInfo = [], $giftNum = 1)
    {
        if (empty($userIds) || !$fromUserId || !$giftInfo) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $userIds = array_merge($userIds, [$fromUserId]);
        $accounts = User::getImAccounts($userIds);
        $fromAccount = $accounts[$fromUserId] ?? '';
        unset($accounts[$fromUserId]);

        $msgInfo = self::buildGiftMsgObj($giftInfo, '已收下礼物，可在“我的账户”查看明细或提现~', $giftNum, GiftLog::STATUS_OK);
        return ImUtil::sendCustomMsg($accounts, $fromAccount, ImUtil::MSG_TYPE_GIFT_RECEIVE, $msgInfo, '', 0);
    }

    /**
     * 发送资料卡片
     */
    static function sendProfileCard($userIds = [], $fromUserId = 0, $profile = null)
    {
        if (empty($userIds) || !$fromUserId || !$profile) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $userIds = array_merge($userIds, [$fromUserId]);
        $accounts = User::getImAccounts($userIds);
        $fromAccount = $accounts[$fromUserId] ?? '';
        unset($accounts[$fromUserId]);
        $msgInfo = [
            'user_id' => $profile->user_id,
            'nick_name' => $profile->nick_name,
            'gender' => $profile->gender,
            'avatar' => $profile->avatar,
//            'city' => UserProfile::checkRoleHost($profile->role) ? '' : $profile->city,
            'city' => '',
            'age' => $profile->age >= 18 ? $profile->age : 18,
            'level' => SettingUserLevel::getUserLevel($profile),
            'tags' => UserTag::getTagNames($profile->user_id),
            //是否关注
            'followed' => 0,
            //是否显示微信入口
            'show_wechat' => 1,
            'posters' => Moment::getMomentPics($profile->user_id, 4),
            //社交资料
        ];
        return ImUtil::sendCustomMsg($accounts, $fromAccount, ImUtil::MSG_TYPE_PROFILE_CARD, $msgInfo, '', 0, 0, 0);
    }

    /**
     * 发送资料卡片（v1.3.3）
     */
    static function sendNewProfileCard($userIds = [], $fromUserId = 0, $profile = null)
    {
        if (empty($userIds) || !$fromUserId || !$profile) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $userIds = array_merge($userIds, [$fromUserId]);
        $accounts = User::getImAccounts($userIds);
        $fromAccount = $accounts[$fromUserId] ?? '';
        unset($accounts[$fromUserId]);
        $isHost = UserProfile::checkRoleHost($profile->role);
        $levelInfo = '';
        if ($profile->gender == 1) {
            $level = SettingUserLevel::getUserLevel($profile);
            $level && $levelInfo .= "豪气Lv" . $level;
            $energy = UserProfile::getEnergy($fromUserId);
            $energy && $levelInfo .= " · 体力" . $energy;
        }
        //标签
        $tags = UserSocialProfile::getProfile($fromUserId, 'tag');
        $posters = Moment::getMomentPics($fromUserId, 4, $profile->poster);
        $loveTags = UserTag::getTagNames($fromUserId, UserTag::TYPE_LOVE);

        //资料都没有，不显示
        if (
            !$tags
            && !$posters
            && !$loveTags
            && !$profile->self_intro
            && !$levelInfo
        ) return false;

        $msgInfo = [
            //用户ID
            'user_id' => $fromUserId,
            //昵称
            'nick_name' => $profile->nick_name,
            //性别
            'gender' => $profile->gender,
            //头像
            'avatar' => $profile->avatar,
            //城市
//            'city' => $isHost ? '' : ($profile->locate_city ?: $profile->city),
            'city' => '',
            //年龄
            'age' => $profile->age,
            //自我介绍
            'self_intro' => $profile->self_intro,
            //标签
            'tags' => $tags,
            //喜欢的标签
            'love_tags' => array_slice($loveTags, 0, 4),
            //照片
            'posters' => array_slice($posters, 0, 5),
            //是否显示微信入口
            'show_wechat' => 0, //Helper::checkTempExcluded($userIds[0], SettingAppHidden::FUNC_WECHAT) ? 0 : 1,
            //等级信息
            'level_info' => $levelInfo,
            //实名
            'real_name' => $isHost ? 1 : 0,
            //真人
            'real_avatar' => UserProfile::checkAvatarVrfied($fromUserId, $profile),
            //是否关注
            'followed' => UserFollow::check($userIds[0], $fromUserId),
        ];
        // log_user_result($userIds[0], $msgInfo);
        return ImUtil::sendCustomMsg($accounts, $fromAccount, ImUtil::MSG_TYPE_PROFILE_CARD, $msgInfo, '', 0, 0, 0);
    }

    /**
     * 礼物消息结构体
     */
    static function buildGiftMsgObj($giftInfo = [], $tip = '', $giftNum = 1, $giftStatus = GiftLog::STATUS_OK)
    {
        return [
            'gift_icon' => AliyunUtil::IMG_HOST_V2 . $giftInfo['icon'],
            'gift_show' => !$giftInfo['show_image'] ? '' : AliyunUtil::IMG_HOST_V2 . $giftInfo['show_image'],
            'gift_id' => $giftInfo['id'],
            'gift_name' => $giftInfo['name'],
            'gift_coin' => $giftInfo['coin'] * $giftNum,
            //礼物状态
            'gift_status' => $giftStatus,
            //礼物状态文案
            'gift_status_text' => GiftLog::STATUS[$giftStatus],
            //系统提示文案
            'gift_tip' => $tip,
            //礼物个数
            'gift_num' => $giftNum,
            //礼物类型
            'gift_type' => $giftInfo['gift_type'] ?? '',
        ];
    }

    /**
     * 聊天系统提示
     */
    static function sendImSystemTip($userId = 0, $chatUserId = 0, $tipContent = '', $tipType = 1)
    {
        if (!$userId || !$chatUserId) return false;
        //获取融云账号
        $accounts = User::getImAccounts([$userId, $chatUserId]);
        return ImUtil::sendInfoTipMsg($accounts[$userId], $tipContent, $accounts[$chatUserId], $tipType);
    }

    /**
     * 发送Push推送
     * @param $pushUserId
     * @param array $msgInfo
     * @param array $sender
     */
    static function sendPush($userIds = [], $pushTitle = '', $byImAccount = false, $extInfo = [])
    {
        if (empty($userIds)) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //如果传入UID数组，则获取融云账号
        if (!$byImAccount) $userIds = array_values(User::getImAccounts($userIds));
        //推送消息
        $pushContent = $pushTitle ?: '你有一条新消息';
        //推送内容
        $content = [
            'push_title' => $pushTitle ?: '你有一条新消息'
        ];
        //发送透传
        return ImUtil::sendPushMsg($userIds, $content, $pushContent);
    }

    /**
     * 系统消息（老版）
     */
    static function pushSystem($userIds = [], $text = '')
    {
        if (empty($userIds) || !$text) return false;
        //兼容非数组
        if (!is_array($userIds)) $userIds = [$userIds];
        //获取融云账号
        $accounts = User::getImAccounts($userIds);
        return ImUtil::systemPushToSingle($accounts, $text, ImUtil::RONG_TEXT_TYPE, $text);
    }

    /**
     * 官方消息（老版）
     */
    static function pushOfficial(
        $toUserIds = [],
        $msgInfo = [],
        $msgType = 'TEXT',
        $msgCate = self::MSG_CATE_SYS
    ) {
        if (!$msgInfo || !$msgType) return false;
        if ('SINGLE_TEXT' == $msgType) {
            $msgContent = [
                'content' => $msgInfo['text'],
                'extra' => '你有一条新消息!',
            ];
        } else {
            $msgContent = [
                'msg_type' => $msgType,
                'title' => $msgInfo['title'] ?? '官方通知',
                'text' => $msgInfo['text'] ?? '你收到一条官方消息~',
                'pic' => $msgInfo['pic'] ?? '',
                'jump_info' => !isset($msgInfo['jump_info']) ? null : [
                    'jump_type' => $msgInfo['jump_info']['jump_type'] ?? 'h5',
                    'jump_text' => $msgInfo['jump_info']['jump_text'] ?? '查看详情',
                    'jump_link' => $msgInfo['jump_info']['jump_link'] ?? '',
                ],
                'msg_user_id' => $msgInfo['msg_user_id'] ?? 0,
            ];
            if (isset($msgInfo['op_result'])) $msgContent['op_result'] = $msgInfo['op_result'];
        }

        log_info(__CLASS__ . "==msgContent==" . json_encode($msgContent));
        //全员官方
        if (empty($toUserIds)) {
            return ImUtil::officialPushToAll($msgContent, ImUtil::MSG_TYPE_SYSMSG, '您有一条新的官方消息~');
        }
        //兼容非数组
        if (!is_array($toUserIds)) $toUserIds = [$toUserIds];
        //获取融云账号
        $accounts = User::getImAccounts($toUserIds);
        return ImUtil::officialPushToSingle(
            $accounts,
            $msgContent,
            ImUtil::MSG_TYPE_SYSMSG,
            '您有一条新的官方消息~',
            $msgCate
        );
    }

    /**
     * 站内通知 - 主播认证通知（新版）
     */
    static function pushHostVerify($userId = 0)
    {
        $msgContent = [
            'title' => '恭喜身份认证成功！',
            'text' => config('message.host_verify'),
            'jump_info' => null,
        ];
        return self::pushOfficial($userId, $msgContent, self::MSG_TYPE_TEXT);
    }

    /**
     * 站内通知 - 微信查看申请结果
     */
    static function pushWechatResult($userId = 0, $result = 1, $info = [])
    {
        $text = $result == 1
            ? "恭喜，{$info['host_name']}同意了您的微信查看申请，赶紧查看Ta的微信吧！"
            : "抱歉，{$info['host_name']}拒绝了您的微信查看申请，支付的金币已退回至您的账户！";
        $msgContent = [
            'title' => "申请查看{$info['host_name']}微信",
            'text' => $text
        ];
        return self::pushOfficial($userId, $msgContent, self::MSG_TYPE_TEXT);
    }

    /**
     * 小助手IM消息 - 主播欢迎
     */
    static function officialImMsg($userId = 0, $type = 'host')
    {
        switch ($type) {
            case 'host':
                $msg = '欢迎你注册平台
希望你在这里能够遇见心意的Ta
下面是提高吸引力，快速找朋友的小秘诀
1、完成真人认证
2、上传头像和5张照片
3、录制一段语音签名
4、完善个人详情资料';
                break;
            default:
                $msg = '欢迎使用APP，有问题随时找我哦～';
        }
        $officialAccount = env('IM_ACCOUNT_KEFU', '');
        $accounts = User::getImAccounts($userId);
        return ImUtil::sendTextMsg($accounts, $msg, $officialAccount);
    }

    /**
     * 站内通知
     */
    static function pushNotice($hostId = 0, $userId = 0, $rewardCoin = 0)
    {
        $profile = UserProfile::getProfile($userId);
        $rechargeTime = date('Y年m月d H:i:s');
        $msgContent = [
            'title' => '速配首充奖励到账通知！',
            'text' => "与你匹配通话过的【{$profile->nick_name}】于{$rechargeTime}充值了，恭喜你获得{$rewardCoin}金币的首充奖励！",
            'jump_info' => null,
        ];
        return Message::pushOfficial($hostId, $msgContent, Message::MSG_TYPE_TEXT);
    }

    /**
     * 站内通知 - 文字站内信（新版）
     */
    static function pushOfficialMsg($userId = 0, $msg = '')
    {
        $ret = self::pushOfficial($userId, [
            'title' => '通知',
            'text' => $msg,
            'jump_info' => null,
        ], self::MSG_TYPE_TEXT);
        return self::sendSlient($userId, 'NEW_NOTICE');
    }

    /**
     * 社交资料编辑
     */
    static function pushSocialProfileNotice($userId, $msg = '')
    {
        $ret = self::pushOfficial($userId, [
            'title' => '通知',
            'text' => $msg ?: '请尽快完善你的社交资料，让更多用户看到哦～',
            'jump_info' => [
                'jump_type' => 'social_profile',
                'jump_text' => '去完善',
            ],
        ], self::MSG_TYPE_TEXT_LINK);
        return self::sendSlient($userId, 'NEW_NOTICE');
    }

    static function testChatCharge($userId = 83, $userId2 = 115)
    {
        $tip = '与认证主播聊天，您需要支付 <font color="#F55363">12金币/条</font>';
        Message::sendImSystemTip($userId, $userId2, $tip, 2);
    }

    /**
     * 发送亲密度提示消息
     */
    static function sendCloseTip($userId = 0, $fromUserId = 0, $closeInfo = [])
    {
        if (!$userId || !$fromUserId || !$closeInfo) return false;
        $accounts = User::getImAccounts([$fromUserId, $userId], -1);
        $msgInfo = [
            'icon' => AliyunUtil::IMG_HOST_V2 . 'icons/close_love.png',
            'close_level' => $closeInfo['level_title'],
            'close_tip' => "恭喜~你们的亲密值已达到{$closeInfo['close_value']}℃，解锁了{$closeInfo['unlock_content']}功能",
            'close_type' => $closeInfo['close_value'] < 1 ? '0' : '1'
        ];
        return ImUtil::sendCustomMsg($accounts[$userId], $accounts[$fromUserId], ImUtil::MSG_TYPE_CLOSE_TIP, $msgInfo, '');
    }

    /**
     * 发送认证的提示消息
     */
    static function sendVryOKTip($userId = 0, $fromUserId = 0)
    {
        if (!$userId || !$fromUserId) return false;
        $accounts = User::getImAccounts([$fromUserId, $userId], -1);
        $msgInfo = [
            'sys_tip' => '对方已通过真人认证，请健康交友',
        ];
        return ImUtil::sendCustomMsg($accounts[$userId], $accounts[$fromUserId], ImUtil::MSG_TYPE_VERYFY_OK, $msgInfo, '', 0, 0, 0);
    }

    /**
     * 站内通知 - 微信申请的消息
     */
    static function pushWechatApply($userId = 0, $msgInfo = [], $type = 'apply')
    {
        $name = $msgInfo['nick_name'] ?? '有用户';
        $coin = intval(ConfigUtil::getWechatCheckCoin() * (1 - ConfigUtil::getConsumeRate($userId)));
        $msgContent = [
            'msg_user_id' => $msgInfo['user_id'] ?? 0,
            'title' => "{$name} 申请查看你的微信",
            'text' => "{$name} 申请查看您的微信号，同意申请后，您可获得{$coin}金币收入；24小时内操作有效，过期视为拒绝申请。",
        ];
        $type == 'ok' && $msgContent['op_result'] = 1;
        $type == 'refuse' && $msgContent['op_result'] = 0;
        //单聊官方消息
        return self::pushOfficial(
            $userId,
            $msgContent,
            self::MSG_TYPE_WECHAT_APPLY,
            self::MSG_CATE_WECHAT_APPLY
        );
    }

    /**
     * 撤回微信申请消息
     */
    static function recallWechatApply($userId = 0, $msgId = '', $sendTime = 0)
    {
        if (!$userId) return false;
        //获取融云账号
        $accounts = User::getImAccounts($userId);
        $fromAccount = ConfigUtil::getOfficialImAccount(3);
        return ImUtil::sysMessageRecall($accounts[$userId], $fromAccount, $msgId, $sendTime);
    }

    /**
     * 小秘书通知
     */
    static function sendXiaomishuMsg($userId = 0, $msg = '')
    {
        if (!$userId) return false;
        // $accounts = User::getImAccounts($userId);
        // $fromAccount = ConfigUtil::getOfficialImAccount(4);
        // return ImUtil::sendTextMsg($accounts, $msg, $fromAccount);
        $msgContent = [
            'title' => "访客消息",
            'text' => $msg
        ];
        return self::pushOfficial($userId, $msgContent, self::MSG_TYPE_TEXT, self::MSG_CATE_XIAOMISHU);
    }

    /**
     * 发送自动消息3件套（语音，图片，文字）
     */
    static function sendProfilePackage($toUserId = 0, $fromUserId = 0, $showSender = 0)
    {
        $msgs = UserAutoMsg::pickQuickChatMsgs($fromUserId);
        if (!$msgs) return false;
        $accounts = User::getImAccounts([$toUserId, $fromUserId]);
        $ret = [];
        foreach ($msgs as $msg) {
            $resultKey = $msg['msg_type'];
            switch ($msg['msg_type']) {
                case 'image':
                    $ret[$resultKey] = ImUtil::sendImageMsg($accounts[$toUserId], $msg['content'], $accounts[$fromUserId], $showSender);
                    break;
                case 'text':
                    $ret[$resultKey] = ImUtil::sendTextMsg($accounts[$toUserId], $msg['content'], $accounts[$fromUserId], $showSender ? -1 : 2);
                    break;
                case 'voice':
                    $ret[$resultKey] = ImUtil::sendVoiceMsg($accounts[$toUserId], $accounts[$fromUserId], $msg['content'], $msg['duration'], $showSender);
                    break;
            }
        }
        log_user_result($fromUserId, "toUserId:{$toUserId}==" . json_encode($ret));
        return $ret;
    }

    /**
     * 清除用户的消息列表透传
     */
    static function cmdClearMsg($userId = 0, $offMinutes = 0)
    {
        $offMinutes = $offMinutes ?: intval(env('CLEAR_MSG_OFFTIME', 86400) / 60);
        return self::sendSlient($userId, 'CLEAR_MSG', ['off_minutes' => $offMinutes]);
    }

    /**
     * 视频通话礼物提醒
     */
    static function sendVideoGiftTip($userId = 0, $giftId = 0)
    {
        $giftItem = Gift::getInfo($giftId);
        if (!$giftItem) return 0;
        return self::sendSlient($userId, 'VIDEO_GIFT_TIP', [
            'gift_id' => $giftItem['id'],
            'gift_name' => $giftItem['name'],
            'gift_icon' => $giftItem['icon'],
            'gift_coin' => $giftItem['coin'],
            'image_host' => AliyunUtil::IMG_HOST_V2,
            'show_image' => $giftItem['show_image'],
            'tip' => '每一次通话都是感情的沉淀 免费赠送礼物给对方；如果对方回赠，你可以获得礼物收益哦~'
        ]);
    }

    /**
     * 发送自定义消息
     * @param int $userId 接收方UID
     * @param int $fromUserId 发送方UID
     * @param string $msgType 类型
     * @param array $extInfo 内容
     * @return array|false|mixed|string
     */

    static function cannotSend($userId = 0, $fromUserId = 0,$msgType='',$extInfo=[])
    {
        if(!$userId || !$fromUserId ) return false;
        if(empty($extInfo)) return false;
        $msgType =  empty($msgType) ?  ImUtil::MSG_TYPE_COIN_NOT_ENOUGH : $msgType;
        //消息数据
        $accounts = User::getImAccounts([$fromUserId, $userId]);
        //发送自定义消息
        return ImUtil::sendCustomMsg($accounts[$userId], $accounts[$fromUserId], $msgType, $extInfo, '', 0, 0, 0);
    }

    /**
     * 自定义消息ID
     * @param string $userImId
     * @param string $fromUserImId
     * @param string $msgType
     * @param array $extInfo
     * @return array|false|mixed|string
     */
    static function cannotImIdSend($userImId = '', $fromUserImId = '',$msgType='',$extInfo=[])
    {
        if(!$userImId || !$fromUserImId || empty($extInfo) || !$msgType) return false;
        //发送自定义消息
        return ImUtil::sendCustomMsg($userImId, $fromUserImId, $msgType, $extInfo, '', 0, 0, 0);
    }

    static function sendXiaomishuCustomMsg($userId = 0, $msg = '',$title='')
    {
        if (!$userId) return false;
        $msgContent = [
            'title' => empty($title) ? '温馨提示' : $title,
            'text' => $msg
        ];
        return self::pushOfficial($userId, $msgContent, self::MSG_TYPE_TEXT, self::MSG_CATE_XIAOMISHU);
    }
}
